package com.henghai.zhenxuan.ui.page.mine;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.MyHarvestBean;
import com.henghai.zhenxuan.ui.page.adapter.MyHarvestAdapter;
import com.henghai.zhenxuan.R;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收货
 */

public class MyHarvestFragment extends BaseFragment {
    private MyHarvestStates mStates;
    private RefreshLayout mRefreshLayout;
    private MyHarvestAdapter adapter;
    private List<MyHarvestBean> taskList;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        taskList = new ArrayList<>();
//        taskList.add(new MyHarvestBean("1","鹅蛋十1枚","123123","2025-08-08","待取件"));
//        taskList.add(new MyHarvestBean("2","鹅蛋十2枚","123123123","2025-08-08","运输中"));
//        taskList.add(new MyHarvestBean("3","鹅蛋十3枚","1231233211","2025-08-08","派送中"));
//        taskList.add(new MyHarvestBean("4","鹅蛋十4枚","312124123","2025-08-08","已打包"));
//        taskList.add(new MyHarvestBean("5","鹅蛋十5枚","324342532","2025-08-08","拣货中"));
        mStates.taskList.set(taskList);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MyHarvestStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        adapter = new MyHarvestAdapter(getContext());
        adapter.setListener(new MyHarvestAdapter.OnItemBtClickListener() {
            @Override
            public void onItemDelClick(MyHarvestBean bean, int postion) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("MyHarvestBean",bean);
                nav().navigate(com.henghai.zhenxuan.R.id.action_myHarvestFragment_to_fastMailDetailsFragment,bundle);
            }
        });

        return new DataBindingConfig(R.layout.fragment_myharvest, BR.vm, mStates)
                .addBindingParam(BR.listener, new ListenerRefreshHandler())
                .addBindingParam(BR.loadListener, new ListenerLoadHandler())
                .addBindingParam(BR.adapter,adapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    public class ClickProxy {
        public void back() {
            Log.d("213213213", "back: ");
            nav().popBackStack();
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mStates.pageNum = 1;
            mRefreshLayout = refreshLayout;
        }
    }

    public class ListenerLoadHandler implements OnLoadMoreListener {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            if (mStates.rows >= Integer.parseInt(mStates.PageSize)) {
                mStates.pageNum++;

            } else {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    public static class MyHarvestStates extends StateHolder {
        public int pageNum = 1;
        public int rows;
        public final String PageSize = "20";
        public final State<List<MyHarvestBean>> taskList = new State<>(new ArrayList<>());
    }
}
